<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT * FROM faction WHERE leader = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( isset( $row[ 'faction_id' ] ) ) {
	$faction_id_tmp = $row[ 'faction_id' ];
	$query2 = "SELECT count(*) FROM race_teams WHERE faction_id = " . $faction_id_tmp;
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	if( $row2[ 'count(*)' ] > 0 ) {
		$status = "Your faction already sponsored a race team.";
	}
	else {
		if( $row[ 'gold' ] > 1000 ) {
			$name = $row[ 'name' ];
			$gold = $row[ 'gold' ];
			$query = "SELECT SUM(population) FROM village JOIN faction_member ON village.ruler_id = faction_member.ruler_id WHERE faction_id = " . $faction_id_tmp . " AND invited = 0 AND requested = 0";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			$faction_pop = $row[ 'SUM(population)' ];
			//print( "factionpop: " . number_format( $faction_pop ) . "<br />\r\n" );
			$query2 = "SELECT SUM(scout+infantry+cavalry+artillery) FROM army JOIN faction_member ON army.ruler_id = faction_member.ruler_id WHERE faction_id = " . $faction_id_tmp . " AND invited = 0 AND requested = 0";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$faction_pop += $row2[ 'SUM(scout+infantry+cavalry+artillery)' ];
			//print( "factionpop: " . number_format( $faction_pop ) . "<br />\r\n" );
			$query2 = "SELECT SUM(recruits) FROM ruler JOIN faction_member ON ruler.ruler_id = faction_member.ruler_id WHERE faction_id = " . $faction_id_tmp . " AND invited = 0 AND requested = 0";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$faction_pop += $row2[ 'SUM(recruits)' ];
			$query2 = "SELECT SUM(population) FROM colony JOIN faction_member ON colony.ruler_id = faction_member.ruler_id WHERE faction_id = " . $faction_id_tmp . " AND invited = 0 AND requested = 0";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$faction_pop += $row2[ 'SUM(population)' ];
	
			$query2 = "SELECT SUM(cavalry) FROM army JOIN faction_member ON army.ruler_id = faction_member.ruler_id WHERE faction_id = " . $faction_id_tmp . " AND invited = 0 AND requested = 0";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$cav_pop = $row2[ 'SUM(cavalry)' ];
			
			$extra_points = strlen( $cav_pop ) + strlen( $faction_pop );
			$agility = 1;
			$rnd = rand( 0, $extra_points * 0.3 );
			$extra_points -= $rnd;
			$agility += $rnd;
			$stamina = 1;
			$rnd = rand( 0, $extra_points * 0.4 );
			$extra_points -= $rnd;
			$stamina += $rnd;
			$acceleration = 1;
			$rnd = rand( 0, $extra_points * 0.5 );
			$extra_points -= $rnd;
			$acceleration += $rnd;
			
			$strength = 1;
			$rnd = rand( 0, $extra_points * 0.6 );
			$extra_points -= $rnd;
			$strength += $rnd;
			$intelligence = 1;
			$intelligence += $extra_points;
			
			mysql_query( "UPDATE faction SET gold = " . ( $gold - 1000 ) . " WHERE faction_id = " . $faction_id_tmp ) or die( "Query failed : " . mysql_error() );
			
			$query = "INSERT INTO race_teams (name, agility, stamina, acceleration, strength, intelligence, faction_id ) " .
					"VALUES( '$name', $agility, $stamina, $acceleration, $strength, $intelligence, $faction_id_tmp )";
			mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$status = "You now have your own faction team!";
		}
		else {
			$status = "You need at least 1,000 gold in your factions reserves.";
		}
	}
}
else {
	$status = "Only faction leaders can sponsor a team.";
}

if( isset( $status ) ) {
	$_SESSION[ 'sponsor_team_status' ] = $status;
}
header( "Location: horseraces.php" );
?>
